library(survey)
library(xtable)

## run "PCM_cleaning.R"

## Create weighted survey design object
milconf_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )

#### GOFO KNOWLEDGE TABLES ####

## Create vectors and percentages for all respondents
mattis_tab <- as.vector(svytable(~ mattis_ans, milconf_design))
mattis_per <- mattis_tab / sum(mattis_tab) * 100

mcmaster_tab <- as.vector(svytable(~ mcmaster_ans, milconf_design))
mcmaster_per <- mcmaster_tab / sum(mcmaster_tab) * 100

petraeus_tab <- as.vector(svytable(~ petraeus_ans, milconf_design))
petraeus_per <- petraeus_tab / sum(petraeus_tab) * 100

powell_tab <- as.vector(svytable(~ powell_ans, milconf_design))
powell_per <- powell_tab / sum(powell_tab) * 100

mcraven_tab <- as.vector(svytable(~ mcraven_ans, milconf_design))
mcraven_per <- mcraven_tab / sum(mcraven_tab) * 100

stavridis_tab <- as.vector(svytable(~ stavridis_ans, milconf_design))
stavridis_per <- stavridis_tab / sum(stavridis_tab) * 100

goldfein_tab <- as.vector(svytable(~ goldfein_ans, milconf_design))
goldfein_per <- goldfein_tab / sum(goldfein_tab) * 100

dunford_tab <- as.vector(svytable(~ dunford_ans, milconf_design))
dunford_per <- dunford_tab / sum(dunford_tab) * 100

milley_tab <- as.vector(svytable(~ milley_ans, milconf_design))
milley_per <- milley_tab / sum(milley_tab) * 100

gofo_all <- rbind(mattis_per, mcmaster_per, petraeus_per, powell_per, mcraven_per,
                  stavridis_per, goldfein_per, dunford_per, milley_per)

cor_all <- c(mattis_per[2], mcmaster_per[2], petraeus_per[2], powell_per[2], mcraven_per[2],
             stavridis_per[2], goldfein_per[1], dunford_per[1], milley_per[1])
gofo_all <- cbind(gofo_all, cor_all)

row.names(gofo_all) <- c("Mattis", "McMaster", "Petraeus", "Powell", "McRaven", "Stavridis",
                         "Goldfein", "Dunford", "Milley")
colnames(gofo_all) <- c("Active", "Retired", "NS", "NHO", "Correct")

## Create table for all respondents
gofo_all_tab <- xtable(gofo_all)
align(gofo_all_tab) <- "|r|cccc|c|"
digits(gofo_all_tab) <- 1
gofo_all_tab # print latex code for table

## Create vectors and percentages for veterans
mattis_tab_v <- as.vector(svytable(~ mattis_ans + VETERAN, milconf_design)[,1])
mattis_per_v <- mattis_tab_v / sum(mattis_tab_v) * 100

mcmaster_tab_v <- as.vector(svytable(~ mcmaster_ans + VETERAN, milconf_design)[,1])
mcmaster_per_v <- mcmaster_tab_v / sum(mcmaster_tab_v) * 100

petraeus_tab_v <- as.vector(svytable(~ petraeus_ans + VETERAN, milconf_design)[,1])
petraeus_per_v <- petraeus_tab_v / sum(petraeus_tab_v) * 100

powell_tab_v <- as.vector(svytable(~ powell_ans + VETERAN, milconf_design)[,1])
powell_per_v <- powell_tab_v / sum(powell_tab_v) * 100

mcraven_tab_v <- as.vector(svytable(~ mcraven_ans + VETERAN, milconf_design)[,1])
mcraven_per_v <- mcraven_tab_v / sum(mcraven_tab_v) * 100

stavridis_tab_v <- as.vector(svytable(~ stavridis_ans + VETERAN, milconf_design)[,1])
stavridis_per_v <- stavridis_tab_v / sum(stavridis_tab_v) * 100

goldfein_tab_v <- as.vector(svytable(~ goldfein_ans + VETERAN, milconf_design)[,1])
goldfein_per_v <- goldfein_tab_v / sum(goldfein_tab_v) * 100

dunford_tab_v <- as.vector(svytable(~ dunford_ans + VETERAN, milconf_design)[,1])
dunford_per_v <- dunford_tab_v / sum(dunford_tab_v) * 100

milley_tab_v <- as.vector(svytable(~ milley_ans + VETERAN, milconf_design)[,1])
milley_per_v <- milley_tab_v / sum(milley_tab_v) * 100

gofo_vet <- rbind(mattis_per_v, mcmaster_per_v, petraeus_per_v, powell_per_v, mcraven_per_v,
                  stavridis_per_v, goldfein_per_v, dunford_per_v, milley_per_v)

cor_vet <- c(mattis_per_v[2], mcmaster_per_v[2], petraeus_per_v[2], powell_per_v[2], mcraven_per_v[2],
             stavridis_per_v[2], goldfein_per_v[1], dunford_per_v[1], milley_per_v[1])
gofo_vet <- cbind(gofo_vet, cor_vet)

row.names(gofo_vet) <- c("Mattis", "McMaster", "Petraeus", "Powell", "McRaven", "Stavridis",
                         "Goldfein", "Dunford", "Milley")
colnames(gofo_vet) <- c("Active", "Retired", "NS", "NHO", "Correct")

## Create table for veterans
gofo_vet_tab <- xtable(gofo_vet)
align(gofo_vet_tab) <- "|r|cccc|c|"
digits(gofo_vet_tab) <- 1
gofo_vet_tab # print latex code for table

## Create vectors and percentages for non-veterans
mattis_tab_n <- as.vector(svytable(~ mattis_ans + VETERAN, milconf_design)[,2])
mattis_per_n <- mattis_tab_n / sum(mattis_tab_n) * 100

mcmaster_tab_n <- as.vector(svytable(~ mcmaster_ans + VETERAN, milconf_design)[,2])
mcmaster_per_n <- mcmaster_tab_n / sum(mcmaster_tab_n) * 100

petraeus_tab_n <- as.vector(svytable(~ petraeus_ans + VETERAN, milconf_design)[,2])
petraeus_per_n <- petraeus_tab_n / sum(petraeus_tab_n) * 100

powell_tab_n <- as.vector(svytable(~ powell_ans + VETERAN, milconf_design)[,2])
powell_per_n <- powell_tab_n / sum(powell_tab_n) * 100

mcraven_tab_n <- as.vector(svytable(~ mcraven_ans + VETERAN, milconf_design)[,2])
mcraven_per_n <- mcraven_tab_n / sum(mcraven_tab_n) * 100

stavridis_tab_n <- as.vector(svytable(~ stavridis_ans + VETERAN, milconf_design)[,2])
stavridis_per_n <- stavridis_tab_n / sum(stavridis_tab_n) * 100

goldfein_tab_n <- as.vector(svytable(~ goldfein_ans + VETERAN, milconf_design)[,2])
goldfein_per_n <- goldfein_tab_n / sum(goldfein_tab_n) * 100

dunford_tab_n <- as.vector(svytable(~ dunford_ans + VETERAN, milconf_design)[,2])
dunford_per_n <- dunford_tab_n / sum(dunford_tab_n) * 100

milley_tab_n <- as.vector(svytable(~ milley_ans + VETERAN, milconf_design)[,2])
milley_per_n <- milley_tab_n / sum(milley_tab_n) * 100

gofo_non <- rbind(mattis_per_n, mcmaster_per_n, petraeus_per_n, powell_per_n, mcraven_per_n,
                  stavridis_per_n, goldfein_per_n, dunford_per_n, milley_per_n)

cor_non <- c(mattis_per_n[2], mcmaster_per_n[2], petraeus_per_n[2], powell_per_n[2], mcraven_per_n[2],
             stavridis_per_n[2], goldfein_per_n[1], dunford_per_n[1], milley_per_n[1])
gofo_non <- cbind(gofo_non, cor_non)

row.names(gofo_non) <- c("Mattis", "McMaster", "Petraeus", "Powell", "McRaven", "Stavridis",
                         "Goldfein", "Dunford", "Milley")
colnames(gofo_non) <- c("Active", "Retired", "NS", "NHO", "Correct")

## Create table for non-veterans
gofo_non_tab <- xtable(gofo_non)
align(gofo_non_tab) <- "|r|cccc|c|"
digits(gofo_non_tab) <- 1
gofo_non_tab # print latex code for table